import Page from '../../../common/page';

const app = getApp()

import request from '../../../utils/https.js';
import util from '../../../utils/util.js';

var imgAddCount = 0; //点击选择图片次数
var userId;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio1: '1',
    imageFile: [], // 存储微信返回的图片地址
    imageFile1: [], // 存储微信返回的图片地址
    imageFile2: [], // 存储微信返回的图片地址
    imageData: [], // 存储后台返回的图片地址

    resultDArr: [], //后台返回的图片带顺序码集合
    hideAddBtn: false, // 是否显示上传图片的按钮,
    uploadBtns: true, //上传按钮的显示与隐藏
    checked: false,
    steps: 1,
    roleList: [
      '我是商家',
      '我是线上运营者'
    ],
    Index: 0,
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    userId = wx.getStorageSync('userId');



    wx.login({
      success: (suc) => {
        if (suc.code) {
          wx.request({
            url: getApp().Api_url + 'user/getSk',
            data: {
              userId: userId,
              jsCode: suc.code,
            },
            success(succ) {
              if (succ.data.code == 200) {
                wx.setStorageSync("sKey", succ.data.data);
              } else {
                wx.showToast({
                  title: '登录状态异常,请稍后重试',
                  icon: 'none'
                })
              }
            }
          })
        }
      }
    })

    request._get(getApp().Api_url + 'rank/toAuth', {
      userId: userId
    }, function (res) {

      if (res.data.code == 200) {
        console.log('页面初始化====>', res.data.data)
        let PartnerData = res.data.data;
        let agentType = PartnerData.result; //-1无任何审核记录 0成功 1审核失败 2待审核 3审核成功
        let reason = PartnerData.reason; //失败原因
        console.log('====审核状态===>', agentType)
        that.setData({
          PartnerData: PartnerData,
          agentType: agentType,
          reason: reason
        })

      }
    }, function () {});
  },
  //切换角色 0 商家 1线上运营
  getRole(event) {
    const that = this;
    let Index = event.currentTarget.dataset.index;
    that.setData({
      Index: Index
    })

  },
  //开始认证
  onStart() {
    const that = this;
    userId = wx.getStorageSync("userId");
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.navigateTo({
        url: '/pages/login/login?path=/pages/my/my'
      })
      return;
    }
    that.setData({
      agentType: 'start'
    })
  },


  //重新注册
  onStart2() {
    const that = this;
    that.setData({
      agentType: 'start',
      steps: 1
    })


    /*     request._get(getApp().Api_url + 'userAuthentication/getState', {
          userId: userId
        }, function (res) {
          if (res.data.success == true) {
            console.log('重新注册===>', res.data)

            if (res.data.info == 2) {
              that.setData({
                agentType: 'start',
                steps: 2
              })
            }

            if (res.data.info == 3) {
              that.setData({
                agentType: 'start',
                steps: 3
              })
            }
          }
        }, function () {}); */

  },


  onChange(event) {
    console.log(event.detail)
    this.setData({
      checked: event.detail
    });
  },
  //联系人信息
  getName(e) {
    const that = this;
    console.log('event===>', e.detail.detail.value)

    let namevalue = e.detail.detail.value;
    that.setData({
      namevalue: namevalue
    })
  },
  //企业名称信息
  getCard(e) {
    const that = this;
    console.log('cardvalue===>', e.detail.detail.value)

    let cardvalue = e.detail.detail.value;
    that.setData({
      cardvalue: cardvalue
    })
  },
  /**
   * 资质图  上传
   */
  choosePic2(e) {
    let that = this;
    //图片选择
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths===>', tempFilePaths)
        that.setData({
          imgReverse_img: tempFilePaths[0]
        })
        // 图片上传
        wx.uploadFile({
          url: getApp().Api_url + 'rank/upImg',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success(response) {

            let resultD = JSON.parse(response.data);

            console.log('11111111===>', resultD)

            if (resultD.code == 200) {
              console.log('resultD==>', resultD)
              that.setData({
                imgReverse: resultD.data
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },


  /*
    店铺图
  */
  choosePic1(e) {
    let that = this;
    //图片选择
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths===>', tempFilePaths)

        that.setData({
          imgPositive_img: tempFilePaths[0]
        })

        // 图片上传
        wx.uploadFile({
          url: getApp().Api_url + 'rank/upImg',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success(response) {
            let resultD = JSON.parse(response.data);
            console.log('11111111===>', resultD)
            if (resultD.code == 200) {
              console.log('resultD==>', resultD)
              that.setData({
                imgPositive: resultD.data
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },


  /*
  运营截图
*/
  choosePic3(e) {
    let that = this;
    //图片选择
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log('tempFilePaths===>', tempFilePaths)

        that.setData({
          operating_img: tempFilePaths[0]
        })

        // 图片上传
        wx.uploadFile({
          url: getApp().Api_url + 'rank/upImg',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success(response) {
            let resultD = JSON.parse(response.data);
            console.log('11111111===>', resultD)
            if (resultD.code == 200) {
              console.log('resultD==>', resultD)
              that.setData({
                operatingImg: resultD.data
              })
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
  },

  choosePic(e) {
    let that = this;
    let imageLength = that.data.imageFile.length;

    let imageFile = that.data.imageFile;
    let length = imageFile.length; // 多图上传，分几次上传时，改变状态问题，记录下上一次图片的长度
    let imageData = that.data.imageData;
    let resultDArr = that.data.resultDArr;
    // 显示上传按钮是，就能上传
    // if (!hideAddBtn){


    //图片选择
    wx.chooseImage({
      count: 2, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          uploadBtns: false, //控制视频图片上传按钮
          index: 0, //控制是视频显示还是图片显示
        })

        tempFilePaths.forEach((v, i) => {
          if (imageFile.length < 2) {
            imageFile.push({
              imgUrl: v,
              text: '上传中...'
            })

            // 图片上传
            wx.uploadFile({
              url: getApp().Api_url + 'rank/upImg',
              filePath: v,
              name: 'image',
              header: {
                'content-type': 'multipart/form-data'
              },
              success(response) {



                let resultD = JSON.parse(response.data);

                if (resultD.success == true) {

                  console.log('resultD===>', resultD)

                  imageFile[length + i].text = '已上传';

                  resultDArr.push(resultD)

                  imageData.push(resultD.msg)


                  that.setData({
                    imageFile: imageFile,
                    imageData: imageData,
                    resultDArr: resultDArr
                  })
                }
              },
              fail(res) {
                console.log(res)
              }
            })



          }
        })

        // 是否展示上传按钮
        if (imageFile.length > 1) {
          that.setData({
            hideAddBtn: true
          })
        } else {
          that.setData({
            hideAddBtn: false
          })
        }

        console.log('imageFile===>', imageFile)
        // 图片展示
        that.setData({
          imageFile: imageFile
        })
      }
    })
    // }
  },
  /**
   * 删除图片
   */
  deleteImg(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let imageFile = that.data.imageFile;
    let imageData = that.data.imageData;
    let resultDArr = that.data.resultDArr;
    // 删除第几张
    imageFile.splice(index, 1);
    imageData.splice(index, 1);
    resultDArr.splice(index, 1)
    that.setData({
      imageFile: imageFile,
      imageData: imageData,
      resultDArr: resultDArr
    })
    // 是否展示上传按钮
    if (imageFile.length > 1) {
      that.setData({
        hideAddBtn: true,

      })
    } else if (imageFile.length == 0) {
      that.setData({
        hideAddBtn: false,
        uploadBtns: true,
        index: 0,
      })

    } else {
      that.setData({
        hideAddBtn: false
      })
    }
  },
  getPhone() {
    const that = this;
    if (that.data.checked) {
      that.setData({
        checked: that.data.checked
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请阅读并同意 Partner资质认证协议 ',
      })
    }
  },
  getPhoneNumber(e) {
    const that = this;
    var skeys = wx.getStorageSync("sKey");
    util._getPhoneNumber(e, that, skeys)
  },
  postSubmit() {
    const that = this;
    that.setData({
      disabled: true
    })

    //先看看是什么角色//切换角色 0 商家 1线上运营
    //再看看该填的数据填没填

    if (that.data.Index == 0) { //商家
      if (!that.data.imgPositive) {
        wx.showToast({
          icon: 'none',
          title: '请上传店铺图片',
        })
        that.setData({
          disabled: false
        })
        return false;
      }
      if (!that.data.imgReverse) {
        wx.showToast({
          icon: 'none',
          title: '请上传营业执照图片',
        })
        that.setData({
          disabled: false
        })
        return false;
      }

    } else { //线上运营
      if (!that.data.operatingImg) {
        wx.showToast({
          icon: 'none',
          title: '请上传图片',
        })
        that.setData({
          disabled: false
        })
        return false;
      }
    }
    //验证是否上传正反面 验证姓名 号是否正确
    if (!that.data.cardvalue || !that.data.namevalue) {
      wx.showToast({
        icon: 'none',
        title: '请填写信息',
      })
      that.setData({
        disabled: false
      })
      return false;
    }


    console.log('可以提交')
    request._post(getApp().Api_url + 'rank/partnerAuth', { //提交
      contact: that.data.namevalue, //联系人
      company: that.data.cardvalue, //公司
      shopImg: that.data.imgPositive, //店面图
      licenseImg: that.data.imgReverse, //营业执照图
      groupImg: that.data.operatingImg, //QQ群的截图
      partnerType: that.data.Index, //0-商家 1-线上运营者
      userId: userId,
      phone: wx.getStorageSync('phone')
    }, function (res) {

      if (res.data.code == 200) {
        that.setData({
          steps: 3,
          agentType: 2,
          disabled: false
        })
      } else {

      }
    }, function () {})


  },


  goRules() {
    wx.navigateTo({
      url: '/pages/rules/rules',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    userId = wx.getStorageSync('userId');

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})